package com.freesky.biz.icbc.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson2.JSON;
import com.freesky.biz.icbc.domain.TMobileRecharge;
import com.freesky.biz.icbc.mapper.MobileRechargeMapper;
import com.freesky.biz.icbc.service.IMobileRechargeService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.config.ProjectConfig;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.enums.ExportStatusEnum;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.service.ITExportRecordService;
import com.freesky.system.task.SyncReport;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class MobileRechargeImpl implements IMobileRechargeService {

    @Autowired
    private MobileRechargeMapper mobileRechargeMapper;

    @Autowired
    private ITExportRecordService exportRecordService;

    @Autowired
    private SyncReport syncReport;

    @Override
    public List<TMobileRecharge> findList(TMobileRecharge mobileRecharge) {
        return mobileRechargeMapper.findList(mobileRecharge);
    }

    @Override
    public AjaxResult exportFile(TMobileRecharge mobileRecharge) {

        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("MOBILE_RECHARGE");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        System.out.println(JSON.toJSONString(exportRecord));
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(mobileRecharge, new TMobileRecharge(), IMobileRechargeService.class,
                "findList", "话费充值",Long.parseLong(exportRecord.getId()),sysUser.getUserName());

        return AjaxResult.success();

        /*
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("MOBILE_RECHARGE");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }
        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        System.out.println(JSON.toJSONString(exportRecord));
        exportRecordService.insert(exportRecord);

        //
        String fileName = syncReport.encodingFilename("话费充值");
        ExcelWriter excelWriter = EasyExcel.write(syncReport.getAbsoluteFile(fileName), TMobileRecharge.class).build();
        WriteSheet writeSheet = EasyExcel.writerSheet("话费充值").build();

        Page<Object> page = PageHelper.startPage(1, 10, true);
        List<TMobileRecharge> list = mobileRechargeMapper.findList(mobileRecharge);

        long total = page.getTotal();
        if (total != 0) {
            long totalPageNum = total / pageSize + 1;
            for (int pageNum = 1; pageNum <= totalPageNum; pageNum++) {
                PageHelper.startPage(pageNum, pageSize, false);
                List<TMobileRecharge> mobileRechargeList = mobileRechargeMapper.findList(mobileRecharge);
                excelWriter.write(mobileRechargeList, writeSheet);
            }
        }

        exportRecord.setFileName(fileName);
        exportRecord.setStatus(ExportStatusEnum.SUBMIT_SUCCESS.getValue());
        exportRecord.setFileUrl(ProjectConfig.getDownloadPath() + fileName);
        exportRecordService.update(exportRecord);
         */
    }
}
